//
//  LightViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/4.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "LightViewController.h"

#define kChangLiangChoose [UIImage imageNamed:@"changliang_yixuan"]
#define kChangLiangUnChoose [UIImage imageNamed:@"changliang_weixuan"]

#define kShanshuoChoose [UIImage imageNamed:@"shanshuo_yixuan"]
#define kShanshuoUnChoose [UIImage imageNamed:@"shanshuo_weixuan"]

#define kGuanbiChoose [UIImage imageNamed:@"guanbi_yixuan"]
#define kGuanbiUnChoose [UIImage imageNamed:@"guanbi_weixuan"]

@interface LightViewController ()
{
    
}
@property (nonatomic,strong) NSString *lightType;
@end

@implementation LightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self findPowerFunc];
}

- (void)UISetting{
    _layoutWidth.constant = kScreenSize.width;
    _layoutHeight.constant = 590;
}

#pragma mark - 网络请求

- (void)findPowerFunc{
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] findPowerByEid:kUserId alias:_powerId progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            NSDictionary *dataDic = responseBody[@"data"];
            
            PowerModel *model = [[PowerModel alloc] init];
            NSDictionary *chargingDic = dataDic[@"charging"];
            if ([chargingDic class] != [NSNull class]) {
                [model setValuesForKeysWithDictionary:chargingDic];
            }
            
            ShopModel *shopModel = [[ShopModel alloc] init];
            NSDictionary *shopDic = dataDic[@"store"];
            if ([shopDic class] != [NSNull class]) {
                [shopModel setValuesForKeysWithDictionary:shopDic];
            }
            
            if (model.green_light_status.integerValue == 0) {//关闭
                [_buttonGuanbi setImage:kGuanbiChoose forState:UIControlStateNormal];
                _lightType = @"0";
            }else if (model.green_light_status.integerValue == 1) {
                [_buttonShanshuo setImage:kShanshuoChoose forState:UIControlStateNormal];
                _lightType = @"1";
            }else{
                [_buttonChangliang setImage:kChangLiangChoose forState:UIControlStateNormal];
                _lightType = @"2";
            }
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];

}

//控制绿灯
- (void)contorlLightFunByButtonTag:(NSInteger)tag{
    NSString *status = @"0";
    if (tag == 101) {
        status = @"2";
    }else if (tag == 102){
        status = @"1";
    }else{
        status = @"0";
    }
    
    if ([status isEqualToString:_lightType]) {
        [self alertVCByTitle:[NSString stringWithFormat:@"现在已经是 %@ 状态啦！",_lightType.integerValue == 0 ? @"关闭" : (_lightType.integerValue == 1 ? @"闪烁" : @"常亮")]];
        return;
    }
    
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] contrlLightByAlias:_powerId eid:kUserId status:status progressBlock:nil success:^(id responseBody) {
        if ([responseBody[@"code"] integerValue] == 200) {
            NSString *messageId = responseBody[@"data"];
            __block LightViewController*weakSelf = self;
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.hub hideAnimated:YES];

                [weakSelf checkLinkByMessageId:messageId tag:tag type:status];
            });
        }else{
            [self.hub hideAnimated:YES];
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

//检查链路
- (void)checkLinkByMessageId:(NSString *)messageId tag:(NSInteger)tag type:(NSString *)type{
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] checkPostToPowerByMessage_id:messageId progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            _lightType = type;
            [self alertVCByTitle:@"灯光状态更改完成！"];
            [self buttonTypeByTag:tag];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

#define mark - 点击事件

- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/**
 灯光控制 ； 101 -> 常亮(2) ; 102 -> 闪烁(1) ; 103 -> 关闭(0)

 */
- (IBAction)lightButtonClick:(UIButton *)sender {
    [self contorlLightFunByButtonTag:sender.tag];
}

- (void)buttonTypeByTag:(NSInteger)tag{
    if (tag == 101) {
        [_buttonChangliang setImage:kChangLiangChoose forState:UIControlStateNormal];
        [_buttonShanshuo setImage:kShanshuoUnChoose forState:UIControlStateNormal];
        [_buttonGuanbi setImage:kGuanbiUnChoose forState:UIControlStateNormal];
    }else if (tag == 102){
        [_buttonChangliang setImage:kChangLiangUnChoose forState:UIControlStateNormal];
        [_buttonShanshuo setImage:kShanshuoChoose forState:UIControlStateNormal];
        [_buttonGuanbi setImage:kGuanbiUnChoose forState:UIControlStateNormal];
    }else{
        [_buttonChangliang setImage:kChangLiangUnChoose forState:UIControlStateNormal];
        [_buttonShanshuo setImage:kShanshuoUnChoose forState:UIControlStateNormal];
        [_buttonGuanbi setImage:kGuanbiChoose forState:UIControlStateNormal];
    }
}


- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
